package selef.xiafan.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import selef.xiafan.dao.SysRolePermissionDao;
import selef.xiafan.entity.system.SysPermission;
import selef.xiafan.entity.system.SysRolePermission;
import selef.xiafan.service.ISysRolePermissionS;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author xiafan
 * @version 1.0
 * @date 2021/7/11 20:05
 */
@Service
public class ISysRolePermissionSImpl implements ISysRolePermissionS {
    @Autowired
    private SysRolePermissionDao sysRolePermissionDao;

    @Override
    @Transactional
    public void saveChange(Integer rid, List<SysPermission> menus) {
        sysRolePermissionDao.deleteAllByRid(rid);
        Iterator<SysPermission> iterator = menus.iterator();
        ArrayList<SysRolePermission> list = new ArrayList<>();
        while (iterator.hasNext()) {
            SysRolePermission sysRolePermission = new SysRolePermission();
            sysRolePermission.setRid(rid);
            sysRolePermission.setPid(iterator.next().getId());
            list.add(sysRolePermission);
        }
        sysRolePermissionDao.saveAll(list);
    }
}
